<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>String constructors</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Strings</a> &gt; <a href=
    "string_constructors.html">String constructors</a>
  </div>

  <div class="name-format">
    String constructors
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;string&gt;
  string();
  string( const string&amp; s );
  string( <strong>size_type</strong> length, const char&amp; ch );
  string( const char* str );
  string( const char* str, <strong>size_type</strong> length );
  string( const string&amp; str, <strong>size_type</strong> index, <strong>size_type</strong> length );
  string( <a href=
"../iterators.html">input_iterator</a> start, <a href="../iterators.html">input_iterator</a> end );
  ~string();
</pre>

  <p>The string constructors create a new string containing:</p>

  <ul>
    <li>nothing; an empty string,</li>

    <li>a copy of the given string <em>s</em>,</li>

    <li><em>length</em> copies of <em>ch</em>,</li>

    <li>a duplicate of <em>str</em> (optionally up to <em>length</em>
    characters long),</li>

    <li>a substring of <em>str</em> starting at <em>index</em> and
    <em>length</em> characters long</li>

    <li>a string of characterss denoted by the <em>start</em> and
    <em>end</em> iterators</li>
  </ul>

  <p>For example,</p>
  <pre class="example-code">
   string str1( 5, &#39;c&#39; );
   string str2( &quot;Now is the time...&quot; );
   string str3( str2, 11, 4 );
   cout &lt;&lt; str1 &lt;&lt; endl;
   cout &lt;&lt; str2 &lt;&lt; endl;
   cout &lt;&lt; str3 &lt;&lt; endl;            
</pre>

  <p>displays</p>
  <pre class="example-code">
   ccccc
   Now is the time...
   time         
</pre>

  <p>The string constructors usually run in <a href=
  "../complexity.html">linear time</a>, except the empty constructor,
  which runs in <a href="../complexity.html">constant time</a>.</p>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
